package com.rongcheng.fortestlist.viewPager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongcheng.fortestlist.R;
import com.rongcheng.fortestlist.Utils;
import com.rongcheng.fortestlist.itemline;

import java.util.ArrayList;
import java.util.List;


public class ViewPagerTestActivity extends AppCompatActivity implements View.OnClickListener {

    //    @BindView(R.id.viewPager)
    ViewPagerAdapter viewPagerAdapter;
    LayoutInflater layoutInflater;
    TextView addThree;
    TextView addFour;
    FrameLayout frameLayoutOne, frameLayoutTwo;
    FragmentOne fragmentOne;
    FragmentTwo fragmentTwo;
    FragmentThree fragmentThree;
    FragmentFour fragmentFour;
    FragmentFive fragmentFive;
    Fragmentsix fragmentsix;
    Fragmentseven fragmentseven;
    Fragmenteight fragmenteight;
    FragmentNine fragmentNine;
    FragmentTen fragmentTen;
    //    TestFragmentPagerAdapter fragmentStatePagerAdapter;
    ViewPager viewPager;
    TabLayout tab_layout;

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        Log.e("Act", "onStart");
        super.onStart();
    }


    @Override
    protected void onResume() {
        Log.e("Act", "onResume");
        super.onResume();
    }

    FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_test);
        tab_layout = (TabLayout) findViewById(R.id.tab_layout);
        /**
         * 测试viewpager+fragments
         */
        testViewPager();

        /**
         * 测试viewPager+views +PagerTitleStrip
         */
//        if (savedInstanceState == null) {
//            testViewsPager();
//        }

    }

    private void testViewsPager() {
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setPageMargin(200);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        List<View> views = new ArrayList<>();
        views.add(layoutInflater.inflate(R.layout.view_pager_one, null, false));
        views.add(layoutInflater.inflate(R.layout.view_pager_two, null, false));
        viewPagerAdapter = new ViewPagerAdapter(views);
        viewPager.setAdapter(viewPagerAdapter);
//        addFour = (TextView) findViewById(R.id.add_four);
//            addThree = (TextView) findViewById(R.id.add_three);
//        frameLayoutOne = (FrameLayout) findViewById(R.id.fragment_one);
//        frameLayoutTwo = (FrameLayout) findViewById(R.id.fragment_two);
//        addFour.setOnClickListener(this);
//        addThree.setOnClickListener(this);
//        Log.e("Act", "onCreate");
//        FragmentManager fragmentManager = getSupportFragmentManager();
//        transaction = fragmentManager.beginTransaction();
//        transaction.replace(R.id.fragment_one, fragmentOne, "one");
//        transaction.replace(R.id.fragment_two, fragmentTwo, "two");
//        transaction.commit();
    }

    List<Fragment> fragments = new ArrayList<>();

    private void testViewPager() {
        fragmentOne = new FragmentOne().newInstance("viewPager ");
        fragmentTwo = new FragmentTwo();
        fragmentThree = new FragmentThree();
        fragmentFour = new FragmentFour();
        fragmentFive = new FragmentFive();
        fragmentsix = new Fragmentsix();
        fragmentseven = new Fragmentseven();
        fragmenteight = new Fragmenteight();
        fragmentNine = new FragmentNine();
        fragmentTen = new FragmentTen();
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        fragments.add(fragmentOne);
        fragments.add(fragmentTwo);
        fragments.add(fragmentThree);
        fragments.add(fragmentFour);
        fragments.add(fragmentFive);
        fragments.add(fragmentsix);
        fragments.add(fragmentseven);
        fragments.add(fragmenteight);
        fragments.add(fragmentNine);
        fragments.add(fragmentTen);
        fragments.add(fragmentOne);
//        fragments.add(fragmentTwo);
//        fragments.add(fragmentThree);
//        fragments.add(fragmentFour);
//        fragments.add(fragmentFive);
//        fragments.add(fragmentsix);
//        fragments.add(fragmentseven);
//        fragments.add(fragmenteight);
//        fragments.add(fragmentNine);
//        fragments.add(fragmentTen);
//        fragments.add(fragmentOne);
//        fragments.add(fragmentTwo);
//        fragments.add(fragmentThree);
//        fragments.add(fragmentFour);
//        fragments.add(fragmentFive);
//        fragments.add(fragmentsix);
//        fragments.add(fragmentseven);
//        fragments.add(fragmenteight);
//        fragments.add(fragmentNine);
//        fragments.add(fragmentTen);
//        fragments.add(fragmentOne);
//        fragments.add(fragmentTwo);
//        fragments.add(fragmentThree);
//        fragments.add(fragmentFour);
//        fragments.add(fragmentFive);
//        fragments.add(fragmentsix);
//        fragments.add(fragmentseven);
//        fragments.add(fragmenteight);
//        fragments.add(fragmentNine);
//        fragments.add(fragmentTen);
//        fragments.add(fragmentOne);
//        fragments.add(fragmentTwo);
//        fragments.add(fragmentThree);
//        fragments.add(fragmentFour);
//        fragments.add(fragmentFive);
//        fragments.add(fragmentsix);
//        fragments.add(fragmentseven);
//        fragments.add(fragmenteight);
//        fragments.add(fragmentNine);
//        fragments.add(fragmentTen);
//        fragmentStatePagerAdapter = new TestFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentStatePagerAdapter);
        tab_layout.setupWithViewPager(viewPager);
        tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tab_layout.setSelectedTabIndicatorHeight(0);

//        tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                ((TextView) tab.getCustomView()).setTextColor(ViewPagerTestActivity.this.getResources().getColor(R.color.color_white));
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//                ((TextView) tab.getCustomView()).setTextColor(ViewPagerTestActivity.this.getResources().getColor(R.color.color_333333));
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//                ((TextView) tab.getCustomView()).setTextColor(ViewPagerTestActivity.this.getResources().getColor(R.color.color_white));
//
//            }
//        });

        for (int i = 0; i < fragments.size(); i++) {
            TabLayout.Tab tab = tab_layout.getTabAt(i);
            ViewGroup.LayoutParams layoutParams = new ViewPager.LayoutParams();
            int s = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
            /**tabLayout的源码中 每个TabView有据左右12dp的距离**/
            layoutParams.width = s / 4 - Utils.dip2px(this, 24);
            View rootView = LayoutInflater.from(ViewPagerTestActivity.this).inflate(R.layout.tab_layout, null);
            rootView.setLayoutParams(layoutParams);

            tab.setCustomView(rootView);


        }
        LinearLayout linearLayout = (LinearLayout) tab_layout.getChildAt(0);
//        Drawable drawable1=ContextCompat.getDrawable(this, R.drawable.shape_line);
        Drawable drawable2 = new itemline(this);
//        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.shape_line));
        linearLayout.setDividerDrawable(drawable2);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE |LinearLayout.SHOW_DIVIDER_END);
//        Log.e("frc  padding right", "" + tab_layout.getChildAt(0).getPaddingRight());
//        tab_layout.getChildAt(0).isSelected();
//        ((TextView) tab_layout.getTabAt(0).getCustomView()).setTextColor(getResources().getColor(R.color.color_white));
    }

    FragmentStatePagerAdapter fragmentStatePagerAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {


//                String tag = makeFragmentName(viewPager.getId(), 0);
//                Log.e("frc", "makeFragmentName ;;;" + tag);
//                Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
//                Log.e("frc", "fragment is null ?  " + (fragment == null));
//                FragmentOne fra = new FragmentOne();
//                switch (position) {
//                    case 0:
//                        return fra;
//                    case 1:
//                        return new FragmentTwo();
//                    case 2:
//                        return new FragmentThree();
//                    case 3:
//                        Log.e("frc", (fra ==null) + "");
//                        return new FragmentFour();
//                    default:
//                        return null;
//                }

            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Log.e("frc", container.getId() + "        " + position);
            return super.instantiateItem(container, position);
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }
    };

    private String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }

    void addThreeClick() {
        try {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction tr = fragmentManager.beginTransaction();
            FragmentThree fragmentThree = new FragmentThree();
//            tr.replace(R.id.fragment_one, fragmentThree);
            tr.commit();
        } catch (Exception e) {
            Log.e("Act", e.getMessage().toString());
        }

    }

    void addFourClick() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        FragmentFour fragmentFour = new FragmentFour();
//        ft.replace(R.id.fragment_two, fragmentFour);
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (fragments != null && fragments.size() > 0) {
            fragments.clear();
            fragments = null;
        }
        super.onDestroy();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.add_three:
//                addThreeClick();
//                break;
//            case R.id.add_four:
//                addFourClick();
//                break;
        }

    }


}
